package com.code.bolg.aop;

import com.code.bolg.annotation.ReturnParam;
import com.code.bolg.constant.CookieConstant;
import com.code.bolg.constant.RedisConstant;
import com.code.bolg.constant.ServletPath;
import com.code.bolg.enums.ResultEnum;
import com.code.bolg.exception.BolgExcetion;
import com.code.bolg.utils.ApiReturnUtil;
import com.code.bolg.utils.CookieUtil;
import com.code.bolg.vo.ResultVo;
import javassist.bytecode.SignatureAttribute;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Aspect
@Component
public class AppAop {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Pointcut("execution( * com.code.bolg.controller.*.*(..))")
    public void pointCutAt() {
    }

    @Around("pointCutAt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        Object o = pjp.proceed();//获取返回值
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        ReturnParam annotation = targetMethod.getAnnotation(ReturnParam.class);
        if (annotation != null && !annotation.isPackage() && !annotation.isEnum()) {
            return o;
        }
        if (annotation != null && annotation.isEnum() && o instanceof Enum&&annotation.isPackage()) {
            return ApiReturnUtil.enumResult(o);
        }
        ResultVo msg;
        try {
            msg = ApiReturnUtil.success(o);
        } catch (BolgExcetion e) {
            e.printStackTrace();
            msg = ApiReturnUtil.custonFail(e);
        } catch (Exception e) {
            e.printStackTrace();
            msg = ApiReturnUtil.fail();
        }
        return msg;
    }
}
